<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserTier extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_tiers';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', 'tier_name', 'tier_level', 'experience_points', 'is_current'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'is_current' => 'boolean',
        'tier_achieved_at' => 'datetime',
    ];

    /**
     * 所属用户关系
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * 获取段位描述
     */
    public function getDescriptionAttribute()
    {
        $descriptions = [
            1 => '刚刚开始社交训练的新手',
            2 => '已经掌握基础对话技巧',
            3 => '能够应对多种社交场景',
            4 => '具备高级社交技能',
            5 => '社交技能已达到专家水平'
        ];

        return $descriptions[$this->tier_level] ?? '未知段位';
    }

    /**
     * 获取下一个段位信息
     */
    public function getNextTierInfo()
    {
        $tiers = [
            1 => ['name' => 'Social Seed', 'required_exp' => 0],
            2 => ['name' => 'Dialogue Explorer', 'required_exp' => 100],
            3 => ['name' => 'Conversation Catalyst', 'required_exp' => 300],
            4 => ['name' => 'Social Navigator', 'required_exp' => 600],
            5 => ['name' => 'Influence Master', 'required_exp' => 1000]
        ];

        $nextLevel = $this->tier_level + 1;
        
        if (isset($tiers[$nextLevel])) {
            return [
                'level' => $nextLevel,
                'name' => $tiers[$nextLevel]['name'],
                'required_exp' => $tiers[$nextLevel]['required_exp'],
                'progress' => $this->experience_points / $tiers[$nextLevel]['required_exp']
            ];
        }

        return null; // 已达到最高段位
    }
}